//
//  PFPhotoType.m
//  PrintFamily
//
//  Created by edaihua on 16/2/26.
//  Copyright © 2016年 ___PrintFactory___. All rights reserved.
//

#import "PFPhotoRectManager.h"

@interface PFPhotoRectManager()

@property (nonatomic) PhotoBoarder boarder;

@end

@import Photos;
@implementation PFPhotoRectManager

@synthesize contentZoomOffset = _contentZoomOffset;

const CGFloat photoBoarderWidth = 10.0;

const CGFloat rectSmallRatioHeight2Width = 0.0;
const CGFloat rectLargeRatioHeight2Width = 0.0;

- (PFPhotoRectManager*)initWithType:(PhotoPrintType)type boarder:(PhotoBoarder)boarder asset:(PHAsset*)asset commonManager:(PFCommonRectManager*)manager {
    if (self = [super init]) {
        
        self.commonRectManager = manager;
        self.boarder        = boarder;
        self.zoomScale      = 1.0;
        self.contentOffset  = CGPointZero;
        self.cropPixelRect  = CGRectZero;
        self.contentZoomOffset = CGPointZero;
        [self updatePixelSize:CGSizeMake(asset.pixelWidth, asset.pixelHeight)];
    }
    
    return self;
}

- (CGRect)getImageRectWithoutScale {
    
    CGRect imageRect = CGRectZero;
    
    if (CGSizeEqualToSize(self.imagePixelSize, CGSizeZero) == NO) {
        CGRect targetImageRect  = [self.commonRectManager getTargetPhotoRectInResizeView];
        CGSize imagePixelSize   = self.imagePixelSize;
        
        CGFloat frameHeightWidthRatio = targetImageRect.size.height / targetImageRect.size.width;
        CGFloat imageHeightWidthRatio = imagePixelSize.height / imagePixelSize.width;
        if (imageHeightWidthRatio > frameHeightWidthRatio) {
            CGFloat scaleX           = targetImageRect.size.width / self.imagePixelSize.width;
            CGFloat imageRectHeight  = imagePixelSize.height * scaleX;
            CGFloat imageRectOriginX = targetImageRect.origin.x;
            CGFloat imageRectOriginY = targetImageRect.origin.y - (imageRectHeight - targetImageRect.size.height) / 2;
            imageRect = CGRectMake(imageRectOriginX, imageRectOriginY, targetImageRect.size.width, imageRectHeight);
        } else if (imageHeightWidthRatio < frameHeightWidthRatio) {
            CGFloat scaleY           = targetImageRect.size.height / imagePixelSize.height;
            CGFloat imageRectWidth   = imagePixelSize.width * scaleY;
            CGFloat imageRectOriginX = targetImageRect.origin.x - (imagePixelSize.width * scaleY - targetImageRect.size.width) / 2;
            CGFloat imageRectOriginY = targetImageRect.origin.y;
            imageRect = CGRectMake(imageRectOriginX, imageRectOriginY, imageRectWidth, targetImageRect.size.height);
        } else {
            imageRect = targetImageRect;
        }
    }
    
    return imageRect;
}


- (void)initCropRect {
    CGSize targetPhotoSize = self.commonRectManager.targetPhotoSize;
    
    
    if (self.isShorterThanTarget) {
        CGFloat scale = self.imagePixelSize.height / targetPhotoSize.height;
        self.cropPixelRect = CGRectMake((self.imagePixelSize.width - targetPhotoSize.width * scale) / 2, 0, targetPhotoSize.width * scale, targetPhotoSize.height * scale);
    } else {
        CGFloat scale = self.imagePixelSize.width / targetPhotoSize.width;
        self.cropPixelRect = CGRectMake(0, (self.imagePixelSize.height - targetPhotoSize.height * scale) / 2, targetPhotoSize.width * scale, targetPhotoSize.height * scale);
    }
}

- (CGRect)getCropRect {
    if (CGRectEqualToRect(self.cropPixelRect, CGRectZero)) {
        [self initCropRect];
    }
    return self.cropPixelRect;
}

- (CGSize)getPhotoSize {
    CGSize photoSize = CGSizeZero;
    
    if (self.zoomScale < 1) {
        CGSize targetPhotoSize = self.commonRectManager.targetPhotoSize;
        CGRect photoRect       = [self getImageRectWithoutScale];
        
        if (self.isShorterThanTarget) {
            photoSize = CGSizeMake(targetPhotoSize.width, photoRect.size.height * self.zoomScale);
        } else {
            photoSize = CGSizeMake(photoRect.size.width * self.zoomScale, targetPhotoSize.height);
        }
    } else {
        photoSize = self.commonRectManager.targetPhotoSize;
    }
    
    return photoSize;
}

- (CGFloat)getPhotoOriginX {
    
    return (self.zoomScale < 1) && !self.isShorterThanTarget ? self.commonRectManager.boarderWidth - self.contentOffset.x : self.commonRectManager.boarderWidth;
}

- (CGFloat)getPhotoOriginY {
    return (self.zoomScale < 1) && self.isShorterThanTarget ? self.commonRectManager.boarderWidth - self.contentOffset.y : self.commonRectManager.boarderWidth;
}

- (void)updatePixelSize:(CGSize)pixelSize {
    self.imagePixelSize = pixelSize;
    self.isShorterThanTarget = (_imagePixelSize.height / _imagePixelSize.width) < (_commonRectManager.targetPhotoSize.height / _commonRectManager.targetPhotoSize.width);
}

- (BOOL)isShorterThanTargetHeight {
    return self.isShorterThanTarget;
}

@end
